package com.ucode.application.controller.acis;

import java.util.List;
import javax.validation.constraints.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ucode.acis.mode.FamousExpert;
import com.ucode.acis.mode.Works;
import com.ucode.acis.service.FamousExpertService;
import com.ucode.acis.service.OffprintService;
import com.ucode.acis.service.WorksService;
import com.ucode.acis.vo.OffprintVo;
import com.ucode.acis.vo.WorksVo;
import com.ucode.application.form.acis.WorksForm;
import com.ucode.application.security.CurrentLoginUserService;
import com.ucode.oss.model.FileInfo;
import com.ucode.oss.service.FileInfoService;
import com.ucode.springboot.starter.web.validator.group.AddGroup;
import com.ucode.springboot.starter.web.validator.group.UpdateGroup;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.base.ResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags="ACIS-作品")
@RestController
@RequestMapping("/acis-works")
@Validated
public class WorksController {
    
    @Autowired
    private FamousExpertService famousExpertService;
    @Autowired
    private WorksService worksService;
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private OffprintService offprintService;
    @Autowired
    private CurrentLoginUserService currentLoginUserService;
    
    @PreAuthorize("hasAuthority('acis:works:list') or hasRole('ACISADMIN') or hasRole('ADMIN')")
    @ApiOperation("作品列表")
    @GetMapping(value = "/list")
    public ResponseResult<Object> list(
            @ApiParam(value ="作品名称",required=false)  @RequestParam(value = "name",required=false) String name,
            @ApiParam(value="页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value="每页记录数",example="20") @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize){
        
        Paginator<WorksVo> paginator = worksService.queryPage(name, null, pageNum, pageSize);
       
        return ResponseResult.success(paginator);
    }
    
    @PreAuthorize("hasAuthority('acis:works:detail') or hasRole('ACISADMIN') or hasRole('ADMIN')")
    @ApiOperation(value="详情信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "String",paramType = "path")
    @GetMapping("/detail/{id}")
    public ResponseResult<Object> detail(@PathVariable(name="id",required=true) Long id){
        Works works = worksService.findById(id);
        if(works == null || works.getDeltag() == 1){
            throw new UcodeServiceException(ResultCodeEnum.NO_RECORD);
        }
        FamousExpert famousExpert = famousExpertService.findById(works.getFamousId());
        FileInfo coverFile = fileInfoService.findById(famousExpert.getCoverId());
        return ResponseResult.success(new com.ucode.application.vo.acis.WorksVo(works,famousExpert, coverFile));
    }
    
    @PreAuthorize("hasAuthority('acis:works:detail') or hasRole('ACISADMIN') or hasRole('ADMIN')")
    @ApiOperation("作品单行本列表")
    @GetMapping(value = "/offprints")
    public ResponseResult<Object> works(
            @ApiParam(value ="作品Id",required=true)  @RequestParam(value = "worksId",required=true) Long worksId,
            @ApiParam(value ="作品名称",required=false)  @RequestParam(value = "name",required=false) String name,
            @ApiParam(value="页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value="每页记录数",example="20") @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize){
        
        Paginator<OffprintVo> paginator = offprintService.queryPage(name, worksId, null, pageNum, pageSize);
        return ResponseResult.success(paginator);
    }
    
    @PreAuthorize("hasAuthority('acis:works:edit') or hasRole('ACISADMIN') or hasRole('ADMIN')")
    @ApiOperation(value="根据主键查询")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "String",paramType = "path")
    @GetMapping("/getById/{id}")
    public ResponseResult<Object> getById(@PathVariable(name="id",required=true) Long id){
        Works works = worksService.findById(id);
        if(works == null || works.getDeltag() == 1){
            throw new UcodeServiceException(ResultCodeEnum.NO_RECORD);
        }
        FamousExpert famousExpert = famousExpertService.findById(works.getFamousId());
        FileInfo coverFile = fileInfoService.findById(famousExpert.getCoverId());
        return ResponseResult.success(new com.ucode.application.vo.acis.WorksVo(works,famousExpert, coverFile));
    }
    
    @PreAuthorize("hasAuthority('acis:works:edit') or hasRole('ACISADMIN') or hasRole('ADMIN')")
    @ApiOperation("编辑作品名家选择列表")
    @GetMapping(value = "/famousExperts")
    public ResponseResult<Object> famousExperts(
            @ApiParam(value ="关键字",required=false)  @RequestParam(value = "keywords",required=false) String keywords,
            @ApiParam(value="页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value="每页记录数",example="20") @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize){
        
        Paginator<FamousExpert> paginator = famousExpertService.queryPage(keywords, null, null, pageNum, pageSize);
       
        return ResponseResult.success(paginator);
    }
    
    @PreAuthorize("hasAuthority('acis:works:edit') or hasRole('ACISADMIN') or hasRole('ADMIN')")
    @ApiOperation(value="创建")
    @PostMapping("/create")
    public ResponseResult<Object> create(@Validated({AddGroup.class}) @RequestBody WorksForm form) {
        
        Works works = form.build();
        int i = worksService.insert(works);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }
    
    @PreAuthorize("hasAuthority('acis:works:edit') or hasRole('ACISADMIN') or hasRole('ADMIN')")
    @ApiOperation(value="更新")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String",paramType = "path")
    @PostMapping("/update/{id}")
    public ResponseResult<Object> update(@PathVariable(name="id",required=true) Long id,
            @Validated({UpdateGroup.class}) @RequestBody WorksForm form) {
        
        Works works = form.build();
        works.setId(id);
        int i = worksService.update(works);
        
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }
    
    @PreAuthorize("hasAuthority('acis:works:delete') or hasRole('ACISADMIN') or hasRole('ADMIN')")
    @ApiOperation(value="删除")
    @PostMapping("/delete")
    public ResponseResult<Object> delete(
            @ApiParam(value="Ids",required=true) @Size(min = 1,max=20,message="一次能操作1-20条记录")@RequestParam(value = "ids", required=true) List<Long> ids) {
        Long currentUserId = currentLoginUserService.getCurrentUserId();
        worksService.delete(ids);
        return ResponseResult.success();
    }
    
}
